import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}


object line_num {
  def main(args: Array[String]): Unit = {
    val sparkconf = new SparkConf()
      .setMaster("local[*]")
    val sparksession = SparkSession
      .builder()
      .config(sparkconf)
      .enableHiveSupport()
      .appName("price_ptv")
      .getOrCreate()

    var df = sparksession.sql(
      """
       SELECT
        |    CASE
        |        WHEN sales_chan_name = 'offline' THEN '线下'
        |        WHEN sales_chan_name = 'online' THEN '线上'
        |    END AS line,
        |    SUM(ord_qty) AS total_demand
        |FROM
        |    db_produce.tb_produce
        |GROUP BY
        |    CASE
        |        WHEN sales_chan_name = 'offline' THEN '线下'
        |        WHEN sales_chan_name = 'online' THEN '线上'
        |    END
        |ORDER BY
        |    line;
        |""".stripMargin
    )
    val line_view = df.createTempView("line_view")
    val line_num = sparksession.sql("select line,total_demand from line_view ")
    //dws_cardio_age.show()
    //-将结果进行存储，存储到 hive 数据库中

    line_num.write.mode(SaveMode.Overwrite).saveAsTable("db_produce.line_num")



    sparksession.stop()
    sparksession.close()
  }
}
